<template>
  <div>
    <div class="login-box">
      <p class="title">小裙纸后台管理</p>
      <el-form :model="user" :rules="rules" ref="form" label-width="0px">
        <el-form-item label="" prop="phone">
          <el-input v-model="user.phone" placeholder="用户名"/>
        </el-form-item>
        <el-form-item label="" prop="password">
          <el-input v-model="user.password" placeholder="密码" type="password"/>
        </el-form-item>
        <el-form-item>
          <div class="button">
            <el-button type="primary" @click="login">登录</el-button>
            <el-button type="danger" @click="resetForm">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import menus from '@/config/menu'
export default {
  name: 'login',
  data () {
    return {
      user: {
        phone: '',
        password: ''
      },
      rules: {
        phone: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      },
      firstMenu: [],
      secondMenu: []
    }
  },
  methods: {
    login () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.axios.post('/login/loginHandle', this.qs.stringify(this.user))
            .then(res => {
              if (res.data.status === 1) {
                window.sessionStorage.setItem('sessionId', res.data.data.key)
                window.sessionStorage.setItem('user', JSON.stringify(res.data.data))
                this.getMenus()
              } else {
                this.$message.error(res.data.msg)
              }
            })
        } else {
          this.$message.error('请正确输入用户名密码')
        }
      })
    },

    getMenus () {
      this.axios.get('/Rbac/node')
        .then(res => {
          if (res.data.status === 1) {
            let temp = []
            let path = []
            res.data.data.forEach((item, index) => {
              if (this.firstMenu['id' + item.id]) {
                this.firstMenu['id' + item.id].title = item.title
                temp.push(this.firstMenu['id' + item.id])
                item.child.forEach(e => {
                // item.children.forEach(e => {
                  if (this.secondMenu['id' + e.id]) {
                    this.secondMenu['id' + e.id].title = e.title
                    temp[index].children.push(this.secondMenu['id' + e.id])
                    path.push(this.secondMenu['id' + e.id].path)
                  }
                })
              }
            })
            window.sessionStorage.setItem('menu', JSON.stringify(temp))
            window.sessionStorage.setItem('path', JSON.stringify(path))
            this.$router.push(path[0])
          }
        })
    },

    resetForm () {
      this.$refs['form'].resetFields()
    }
  },
  created () {
    let temp = []
    menus.forEach(item => {
      this.firstMenu['id' + item.id] = {
        title: item.title,
        icon: item.icon,
        children: []
      }
      temp.push(...item.children)
    })
    temp.forEach(item => {
      this.secondMenu['id' + item.id] = {
        title: item.title,
        path: item.path
      }
    })
  }
}
</script>

<style scoped>
  .title{
    font-size: 20px;
    display: inline-block;
    line-height: 60px;
    color: #666;
    font-weight: bold;
    text-align: center;
    width: 100%;
  }
  .login-box{
    width: 300px;
    height: 200px;
    margin: 100px auto 0;
    border: 1px solid #ccc;
    border-radius: 8px;
    padding: 16px;
  }
  .button{
    display: flex;
  }
  .button button{
    flex-grow: 1;
  }
</style>
